package com.broada.apm.utils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * 
 * Json转化工具类
 * 
 * @author louyi Create By 2014-11-21 上午10:56:20
 */
public class JsonUtil {
	private static ObjectMapper mapper = new ObjectMapper();

	static {
		DateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZ");
		mapper.setDateFormat(myDateFormat);
		mapper.setSerializationInclusion(Include.NON_NULL);
		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	public static Object json2obj(String json, Class<?> clas) throws JsonParseException, JsonMappingException, IOException {
		if (json == null)
			return null;
		return mapper.readValue(json, clas);
	}

	public static String obj2json(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
		if (obj == null)
			return null;
		return mapper.writeValueAsString(obj);
	}

	/**
	 * 
	 * @param <T>
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	public static <T> Map<String, Object> json2map(String jsonStr) throws Exception {
		return mapper.readValue(jsonStr, Map.class);
	}

	/**
	 * json string convert to map with javaBean
	 * 
	 * @param <T>
	 * @param jsonStr
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) throws Exception {
		Map<String, Map<String, Object>> map = mapper.readValue(jsonStr, new TypeReference<Map<String, T>>() {
		});
		Map<String, T> result = new HashMap<String, T>();
		for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
			result.put(entry.getKey(), map2obj(entry.getValue(), clazz));
		}
		return result;
	}

	/**
	 * json array string convert to list with javaBean
	 */
	public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception {
		List<Map<String, Object>> list = mapper.readValue(jsonArrayStr, new TypeReference<List<T>>() {
		});
		List<T> result = new ArrayList<T>();
		for (Map<String, Object> map : list) {
			result.add(map2obj(map, clazz));
		}
		return result;
	}

	/**
	 * map convert to javaBean
	 * 
	 * @param <T>
	 * @param map
	 * @param clazz
	 * @return
	 */
	public static <T> T map2obj(Map map, Class<T> clazz) {
		return mapper.convertValue(map, clazz);
	}
	
	public static String map2json(Map map) throws Exception{
		return mapper.writeValueAsString(map);
	}
}